package org.jeecg.modules.clockRecord.scheduled;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.classes.entity.Classes;
import org.jeecg.modules.classes.service.IClassesService;
import org.jeecg.modules.clockRecord.entity.ClockRecord;
import org.jeecg.modules.clockRecord.service.IClockRecordService;
import org.jeecg.modules.employee.entity.Employee;
import org.jeecg.modules.employee.service.IEmployeeService;
import org.jeecg.modules.scheduling.entity.Scheduling;
import org.jeecg.modules.scheduling.service.ISchedulingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.time.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Configuration
@EnableScheduling
@Slf4j
public class MyScheduled {

    @Autowired
    private ISchedulingService schedulingService;
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IClassesService classesService;
    @Autowired
    private IClockRecordService clockRecordService;

    @Scheduled(cron = "0 0 1 * * ?")
    public void scheduled() {
        log.info("开始执行生成每日打卡记录");
        QueryWrapper<Scheduling> wrapper = new QueryWrapper<>();
        wrapper.eq("DATE(date)", DateUtils.formatDate());
        List<Scheduling> list = schedulingService.list(wrapper);
        createAndSaveClockRecord(list);
    }

    private void createAndSaveClockRecord(List<Scheduling> list) {
        List<ClockRecord> saveList = new ArrayList<>();
        // 获取当前日期
        LocalDate today = LocalDate.now();
        // 将当前日期时间设置为今天的开始时间，并转换为系统默认时区的ZonedDateTime
        ZonedDateTime startOfDay = today.atStartOfDay(ZoneId.systemDefault());
        // 转换为Date对象
        Date date = Date.from(startOfDay.toInstant());
        for (Scheduling scheduling : list) {
            String admitGuid = scheduling.getAdmitGuid();
            String classesId = scheduling.getClassesId();
            Employee byAdmitGuid = employeeService.getByAdmitGuid(admitGuid);
            Classes byId = classesService.getById(classesId);
            ClockRecord clockRecord = new ClockRecord();
            clockRecord.setDate(date)
                    .setAdmitGuid(admitGuid).setDept(byAdmitGuid.getDept()).setClassesId(classesId)
                    .setAdmitName(byAdmitGuid.getName()).setFaceUrl(byAdmitGuid.getFaceUrl())
                    .setClasses(byAdmitGuid.getClasses()).setDeputySupervisor(byAdmitGuid.getDeputySupervisor())
                    .setWorkNumber(byAdmitGuid.getWorkNumber()).setClassesType(byId.getClassesType());
            saveList.add(clockRecord);
        }
        clockRecordService.saveBatch(saveList);
        log.info("生成成功");
    }

}
